package com.cs.beneficios.web.action;
import com.cs.beneficios.business.entity.RepDetBeneficiarioDAO;
import com.cs.beneficios.business.model.RepAtributoVO;
import com.cs.beneficios.business.model.RepDetBOTVO;
import com.cs.beneficios.business.model.RepDetBeneficiarioVO;
import com.cs.beneficios.common.ConnectionFactory;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
//import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionErrors;
import java.io.IOException;

import java.util.Collection;

import javax.servlet.ServletException;

public class GenerationReportBeneficiarioAction extends Action 
{
  /**
   * This is the main action called from the Struts framework.
   * @param mapping The ActionMapping used to select this instance.
   * @param form The optional ActionForm bean for this request.
   * @param request The HTTP Request we are processing.
   * @param response The HTTP Response we are processing.
   * @return 
   * @throws java.io.IOException
   * @throws javax.servlet.ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
  {
    HttpSession session = request.getSession();
    List lBeneficiariosEncontrado = (List) session.getAttribute("Beneficiario");
    //ConnectionFactory cf = new ConnectionFactory("beneficios","beneficios");
	ConnectionFactory cf = new ConnectionFactory();
    List lFiltros = new LinkedList();
    List lBeneficiario = new LinkedList();
    List lAtributos = new LinkedList();
    
    List lEspecificos = new LinkedList();
    List lGenerales = new LinkedList();
    
    lFiltros = (List)session.getAttribute("ListaFiltrado");
    session.setAttribute("listafiltros",lFiltros);
    lBeneficiario = (List)session.getAttribute("listabeneficiario");
    lAtributos = (List)session.getAttribute("listaatributos");
  
    if((Object)lBeneficiario != null)
    {
       int tamano1 = lBeneficiario.size();
       for(int i=0;i<tamano1;i++)
       {
         RepAtributoVO repatriVO = (RepAtributoVO)lBeneficiario.get(i);
         String valor1 ="atrbeneficiario"+i;;
         String valor2 = request.getParameter(valor1);
         if(valor2 != null)
         {
           RepAtributoVO repespecVO = new RepAtributoVO();
           repespecVO.setNombre(repatriVO.getNombre());
           repespecVO.setEtiqueta(repatriVO.getEtiqueta());
           lGenerales.add(repespecVO);
         }
       }
       session.setAttribute("listageneral",lGenerales);
       if((Object)lAtributos != null)
       {
         if((Object)lAtributos != null)
         {
           int tamano = lAtributos.size();
           for(int i=0;i<tamano;i++)
           {
             RepAtributoVO repatriVO = (RepAtributoVO)lAtributos.get(i);
             //Long valor1 =(repatriVO.getCodigo());
             String valor = "atributo"+i;
             String valor2 = request.getParameter(valor);
             if(valor2 != null)
             {
               RepAtributoVO repespecVO = new RepAtributoVO();
               repespecVO.setCodigo(repatriVO.getCodigo());
               repespecVO.setEtiqueta(repatriVO.getEtiqueta());
               lEspecificos.add(repespecVO);
             }
           }
           session.setAttribute("listaEspecificos",lEspecificos);
         }else
         {
           session.setAttribute("msg","Seleccione alguna opcion en la lista de beneficios");
         }
       }
    }else
    {
      session.setAttribute("msg","Seleccione alguna opcion en la lista del beneficiario");
    }
    
    if(lGenerales.size() != 0 || lEspecificos.size() != 0)
    {
      
        int itamanoEsp = lEspecificos.size();
        String tamanoEsp=String.valueOf(itamanoEsp);
        int itamanoGen = lGenerales.size();
        String tamanoGen=String.valueOf(itamanoGen);
        
        session.setAttribute("TamanoEsp",tamanoEsp);
        session.setAttribute("TamanoGen",tamanoGen);
        
        RepDetBOTVO criterea  = new RepDetBOTVO();
        
        RepDetBeneficiarioDAO rdbDAO = new RepDetBeneficiarioDAO(cf); 
       
        try{
            RepDetBeneficiarioVO RepDetBene = new RepDetBeneficiarioVO();
            RepDetBene = (RepDetBeneficiarioVO)session.getAttribute("Parametros");
            List reporte = (List)rdbDAO.getReporte((Collection)lGenerales,(Collection)lFiltros,cf, RepDetBene);
            
            if(RepDetBene.getCodePrograma() == null || RepDetBene.getCodeSubprograma() == null){
                RepDetBene.setCantidadBeneficiarios(rdbDAO.getCantidadBeneficiarios(lFiltros, cf, RepDetBene));
                
            } else {
                RepDetBene.setCantidadBeneficiarios(new Integer(reporte.size()));
            }
           
            session.setAttribute("Parametros", RepDetBene);
            
           //cf.closeConnection(rdbDAO.getConn());
           session.setAttribute("listareporte",reporte);
        }catch(SQLException e)
        {
			System.err.println("/******** TDSUB - cwsub");
			e.printStackTrace();
			System.err.println("TDSSPE - cwsub ********/");
        }
       
    }else
    {
      request.setAttribute("msg","Seleccione alguna opcion en las listas de atributos del beneficio o del beneficiario");
    }
    
    
    
    
    
    return mapping.findForward("success");
  }
}